package cn.wd.web.controller;


import cn.wd.domain.ConfigInfo;
import cn.wd.page.PageResult;
import cn.wd.qo.ConfigInfoQueryObject;
import cn.wd.service.IConfigInfoService;
import cn.wd.vo.JsonResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/configInfo")
public class ConfigInfoController {
    
    private final IConfigInfoService service;

    public ConfigInfoController(IConfigInfoService service) {
        this.service = service;
    }

    @RequestMapping("/list")
    public JsonResult<PageResult<ConfigInfo>> query(ConfigInfoQueryObject qo) {
        return JsonResult.success(service.query(qo));
    }

    @RequestMapping("/all")
    public JsonResult<List<ConfigInfo>> all() {
        return JsonResult.success(service.selectAll());
    }

    @RequestMapping("/saveOrUpdate")
    public JsonResult<?> saveOrUpdate(@RequestBody ConfigInfo configInfo) {
        if (configInfo.getId() != null) {
            service.updateById(configInfo);
        } else {
            service.save(configInfo);
        }

        return JsonResult.success();
    }

    @RequestMapping("/delete")
    public JsonResult<?> delete(Long id) {
        if (id == null) {
            return JsonResult.failed("删除的 id 不能为空!");
        }

        service.deleteById(id);
        return JsonResult.success();
    }

    /**
     * 批量删除系统配置属性
     * @param deletedIds ids 数组
     * @return
     */
    @RequestMapping("/batchDelete")
    public JsonResult<?> batchDelete(@RequestBody(required=false) Long[] deletedIds) {
        service.batchDelete(deletedIds);
        return JsonResult.success();
    }
}
